package com.sprites;

import com.bricks2break.Player;
import com.bricks2break.Statics;

import sheep.collision.CollisionListener;
import sheep.game.Sprite;

public class Paddle extends Sprite implements CollisionListener{
	private int width;
	private int height;
	private Player owner = null;
	
	/**
	 * Paddle class constructor
	 * @param verticalPosition		desired y position of paddle 
	 */
	public Paddle(float verticalPosition) {
		super(Statics.image_paddle);
		setPosition(Statics.SCREEN_WIDTH/2, verticalPosition);
		height = (int) Statics.image_paddle.getHeight();
		width = (int) Statics.image_paddle.getWidth();
		addCollisionListener(this);
		setGroup((int) Statics.COLLISION_GROUP);
	}
	
	
	public void setOwner(Player player){
		this.owner = player;
	}
	
	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}
	
	/**
	 * Method that returns whether the paddle is under touch by the finger
	 * @param x		the x coordinate
	 * @param y		the y coordinate
	 * @return		true if under touch and false otherwise
	 */
	public boolean isUnderTouch(int x,int y){
		// Check if x & y is inside the paddle bounds
		if(x >= getX() - width * (0.5 + Statics.PADDLE_TOUCH_AREA_FACTOR) && x <= getX() + width * (0.5 + Statics.PADDLE_TOUCH_AREA_FACTOR) 
				&& y >= getY() - height * (0.5 + Statics.PADDLE_TOUCH_AREA_FACTOR) && y <= getY() + height*(0.5 + Statics.PADDLE_TOUCH_AREA_FACTOR)){
			// touchedDx = getPosition().getX()-x;
			return true;
		}
		return false;
	}
	
	/**
	 * Method that sets the position of the paddle 
	 * @param f		the new position for the paddle
	 */
	public void setPositionX(float f){
		// Make sure x & y is not outside screen
		f = Math.max(f, width/2); //left
		f = Math.min(f, Statics.SCREEN_WIDTH - width/2); //right
		
		// update paddle position
		setPosition(f, getPosition().getY());
	}

	/**
	 * Update method
	 */
	public void update(float dt) {
		super.update(dt);
	}
	
	/**
	 * Draw method
	 */
	public void draw(android.graphics.Canvas canvas) {
		canvas.drawRect(getX() + getWidth()/2, getY() - getHeight()/2, getX() + getWidth()/2 - Statics.PADDLE_DIRECTIONAL_BOUNCE_AREA, getY() + getHeight()/2, Statics.gray);
		canvas.drawRect(getX() - getWidth()/2 + Statics.PADDLE_DIRECTIONAL_BOUNCE_AREA, getY() - getHeight()/2, getX() + getWidth()/2 - Statics.PADDLE_DIRECTIONAL_BOUNCE_AREA, getY() + getHeight()/2, Statics.white);
		canvas.drawRect(getX() - getWidth()/2, getY() - getHeight()/2, getX() - getWidth()/2 + Statics.PADDLE_DIRECTIONAL_BOUNCE_AREA, getY() + getHeight()/2, Statics.gray);
	} 
	
	/**
	 * Method that checks if the paddle collides with a ball and changes the owner of the ball
	 */
	public void collided(Sprite a, Sprite b) {
		if (b instanceof Ball){
			((Ball) b).setCurrentOwner(owner);
		}
	}
}